package com.dieusoft.box.trade.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dieusoft.box.info.entity.JzxDefaultCostDetails;
import com.dieusoft.box.info.mapper.JzxDefaultCostDetailsMapper;
import com.dieusoft.box.trade.entity.CostInfo;
import com.dieusoft.box.trade.entity.JzxTrade;
import com.dieusoft.box.trade.mapper.CostInfoMapper;
import com.dieusoft.box.trade.mapper.JzxTradeMapper;
import com.dieusoft.box.trade.service.IJzxTradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 集装箱贸易表
 * @Author: jeecg-boot
 * @Date:   2020-10-22
 * @Version: V1.0
 */
@Service
public class JzxTradeServiceImpl extends ServiceImpl<JzxTradeMapper, JzxTrade> implements IJzxTradeService {

	@Resource
	private JzxTradeMapper jzxTradeMapper;
	@Autowired
	private JzxDefaultCostDetailsMapper jzxDefaultCostDetailsMapper;
	@Autowired
	private CostInfoMapper costInfoMapper;
	
	@Override
	@Transactional
	public void saveMain(JzxTrade jzxTrade, List<JzxDefaultCostDetails> jzxDefaultCostDetailsList, List<CostInfo> costInfoListYs, List<CostInfo> costInfoListYf) {
		jzxTradeMapper.insert(jzxTrade);
		if(jzxDefaultCostDetailsList!=null && jzxDefaultCostDetailsList.size()>0) {
			for(JzxDefaultCostDetails entity:jzxDefaultCostDetailsList) {
				//外键设置
				entity.setCostId(jzxTrade.getId());
				jzxDefaultCostDetailsMapper.insert(entity);
			}
		}
		if(costInfoListYs!=null && costInfoListYs.size()>0) {
			for(CostInfo entity:costInfoListYs) {
				//外键设置
				entity.setBuisId(jzxTrade.getId());
				entity.setType("ys");
				costInfoMapper.insert(entity);
			}
		}
		if(costInfoListYf!=null && costInfoListYf.size()>0) {
			for(CostInfo entity:costInfoListYf) {
				//外键设置
				entity.setBuisId(jzxTrade.getId());
				entity.setType("yf");
				costInfoMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(JzxTrade jzxTrade,List<JzxDefaultCostDetails> jzxDefaultCostDetailsList, List<CostInfo> costInfoListYs, List<CostInfo> costInfoListYf) {
		jzxTradeMapper.updateById(jzxTrade);
		
		//1.先删除子表数据
		jzxDefaultCostDetailsMapper.deleteByMainId(jzxTrade.getId());
		costInfoMapper.deleteByMainId(jzxTrade.getId());
		
		//2.子表数据重新插入
		if(jzxDefaultCostDetailsList!=null && jzxDefaultCostDetailsList.size()>0) {
			for(JzxDefaultCostDetails entity:jzxDefaultCostDetailsList) {
				//外键设置
				entity.setCostId(jzxTrade.getId());
				jzxDefaultCostDetailsMapper.insert(entity);
			}
		}
		if(costInfoListYs!=null && costInfoListYs.size()>0) {
			for(CostInfo entity:costInfoListYs) {
				//外键设置
				entity.setBuisId(jzxTrade.getId());
				entity.setType("ys");
				costInfoMapper.insert(entity);
			}
		}
		if(costInfoListYf!=null && costInfoListYf.size()>0) {
			for(CostInfo entity:costInfoListYf) {
				//外键设置
				entity.setBuisId(jzxTrade.getId());
				entity.setType("yf");
				costInfoMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		jzxDefaultCostDetailsMapper.deleteByMainId(id);
		costInfoMapper.deleteByMainId(id);
		jzxTradeMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			jzxDefaultCostDetailsMapper.deleteByMainId(id.toString());
			costInfoMapper.deleteByMainId(id.toString());
			jzxTradeMapper.deleteById(id);
		}
	}
	
}
